package zk2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HbastUtil111 {
    private static Connection connection=null;
    private static Admin admin=null ;

    static {
        Configuration configuration= HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","hadoop100");

        try {
            //获取连接
            connection = ConnectionFactory.createConnection(configuration);
            //获取HBaseAdmin对象
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void close(Connection connection, Admin admin) {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (admin != null) {
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //判断表是否存在
    static boolean tableExist(String tableName) throws IOException {
        return admin.tableExists(TableName.valueOf(tableName));

    }
    static void createTable(String tableName, String... columnFAmilys){
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
        for (String columnFAmily : columnFAmilys) {
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columnFAmily);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        try {
            admin.createTable(hTableDescriptor);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void putData(String tableName, String rowkey, String cf, String cn, String value) throws IOException {

        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(Bytes.toBytes(rowkey));
        put.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn),Bytes.toBytes(value));
        table.put(put);
        table.close();
    }

    private static void deleteTable(String tableName) throws IOException {
        admin.disableTable(TableName.valueOf(tableName));
        admin.deleteTable(TableName.valueOf(tableName));
    }

    //全表扫描

    static void scanData(String tableName) throws IOException {

        //获取table对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        Scan scan = new Scan();

        ResultScanner scanner = table.getScanner(scan);

        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            //打印获得的数据
            for (Cell cell : cells) {
                System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell))
                        + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell))
                        + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell))
                        + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));
            }

        }

    }






    //获取一行数据（指定列族：列）
    static void getDataByCN(String tableName, String rowkey, String cf, String cn) throws IOException {

        Table table = connection.getTable(TableName.valueOf(tableName));

        Get get = new Get(Bytes.toBytes(rowkey));
//        get.addFamily()//获取一个列族下的所有数据
        get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
        get.setMaxVersions(3);

        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        //打印获得的数据
        for (Cell cell : cells) {
            System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell))
                    + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell))
                    + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell))
                    + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
        table.close();
    }
    //查询一条数据
    private static  Cell[] getData(String tableName, String rowkey) throws IOException {

        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        Get get = new Get(Bytes.toBytes(rowkey));

        Result result = table.get(get);

        Cell[] cells = result.rawCells();

        //打印获得的数据
//        for (Cell cell : cells) {
//            System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell))
//                    + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell))
//                    + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell))
//                    + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));
//        }

        return cells;

        //关闭表连接
//        table.close();
    }

    //删除行row
    public static void deleteMultiRow(String tableName, String... rows) throws IOException{
        Table table = connection.getTable(TableName.valueOf(tableName));
        List<Delete> deleteList = new ArrayList<Delete>();
        for(String row : rows){
            Delete delete = new Delete(Bytes.toBytes(row));
            deleteList.add(delete);
        }
        table.delete(deleteList);
        table.close();
    }



    //删除月销量小于100的
    static void del100(String tableName) throws IOException {

        //获取table对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        Scan scan = new Scan();

        ResultScanner scanner = table.getScanner(scan);

        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            //打印获得的数据
            for (Cell cell : cells) {
                String row = Bytes.toString(CellUtil.cloneRow(cell));
                String info = Bytes.toString(CellUtil.cloneFamily(cell));

                String leixing = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));

                if(leixing.equals("xl") && Integer.parseInt(value)<100){
                    System.out.println(row+" "+info+" "+leixing+" "+value);
                    HbastUtil111.deleteMultiRow("article",row);

                }


//                System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell))
//                        + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell))
//                        + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell))
//                        + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));



            }

        }

    }


    //查询价格在3000-5000之间的数据
    static void qujian(String tableName) throws IOException {

        //获取table对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        Scan scan = new Scan();

        ResultScanner scanner = table.getScanner(scan);

        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            //打印获得的数据
            for (Cell cell : cells) {
                String row = Bytes.toString(CellUtil.cloneRow(cell));
                String info = Bytes.toString(CellUtil.cloneFamily(cell));

                String leixing = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));

                if(leixing.equals("lll") && Integer.parseInt(value)>=3000 && Integer.parseInt(value)<=5000){
                    System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell))
                        + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell))
                        + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell))
                        + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));
                }


//                System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell))
//                        + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell))
//                        + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell))
//                        + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));



            }

        }

    }


    //询所有商品的平均价格，打印到控制台
    static void pingjun(String tableName) throws IOException {

        //获取table对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        Scan scan = new Scan();

        ResultScanner scanner = table.getScanner(scan);

        double count=0.0;
        int i=0;
        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            //打印获得的数据
            for (Cell cell : cells) {
                String row = Bytes.toString(CellUtil.cloneRow(cell));
                String info = Bytes.toString(CellUtil.cloneFamily(cell));

                String leixing = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));

                if(leixing.equals("xl") ){
                 count+=Double.parseDouble(value);

                 i++;

                }


//                System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell))
//                        + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell))
//                        + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell))
//                        + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));

            }

        }

        System.out.println("所有商品的平均价格"+count/i);

    }


    //文本为自行车的总价
    static void zongjia(String tableName) throws IOException {

        //获取table对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        Scan scan = new Scan();

        ResultScanner scanner = table.getScanner(scan);

        double count=0.0;
        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            //打印获得的数据
            for (Cell cell : cells) {
                String row = Bytes.toString(CellUtil.cloneRow(cell));
                String info = Bytes.toString(CellUtil.cloneFamily(cell));

                String leixing = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));

                if(leixing.equals("xl") ){
                    count+=Double.parseDouble(value);


                }


//                System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell))
//                        + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell))
//                        + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell))
//                        + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));



            }

        }

        System.out.println("文本为自行车的总价"+count);

    }


    //使用hbaseAPI查询风华数码店店铺商品价格在100之上的数据，打印到控制台
    static void dianpu(String tableName) throws IOException {

        //获取table对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        Scan scan = new Scan();

        ResultScanner scanner = table.getScanner(scan);

        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            //打印获得的数据
            for (Cell cell : cells) {
                String row = Bytes.toString(CellUtil.cloneRow(cell));
                String info = Bytes.toString(CellUtil.cloneFamily(cell));

                String leixing = Bytes.toString(CellUtil.cloneQualifier(cell));


                if(leixing.equals("name")  ){
                    String value = Bytes.toString(CellUtil.cloneValue(cell));
                    if(value.equals("天际旗舰店"
                    )){
                        Cell[] articles = HbastUtil111.getData("article", row);

                        for (Cell article : articles) {

                            String row1 = Bytes.toString(CellUtil.cloneRow(article));
                            String info1 = Bytes.toString(CellUtil.cloneFamily(article));

                            String leixing1 = Bytes.toString(CellUtil.cloneQualifier(article));
                            String value1 = Bytes.toString(CellUtil.cloneValue(article));


                            System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(article))
                                    + ",CF:" + Bytes.toString(CellUtil.cloneFamily(article))
                                    + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(article))
                                    + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(article)));

                        }
                    }



                }


//                System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell))
//                        + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell))
//                        + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell))
//                        + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));



            }

        }

    }



}
